package com.ruoyi.mall.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import com.ruoyi.mall.domain.bo.CustomerLevelBo;
import com.ruoyi.mall.domain.vo.CustomerLevelVo;
import com.ruoyi.mall.domain.CustomerLevel;
import com.ruoyi.mall.mapper.CustomerLevelMapper;
import com.ruoyi.mall.service.ICustomerLevelService;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 会员等级Service业务层处理
 *
 * @author ruoyi
 * @date 2024-04-24
 */
@RequiredArgsConstructor
@Service
public class CustomerLevelServiceImpl implements ICustomerLevelService {

    private final CustomerLevelMapper baseMapper;

    /**
     * 查询会员等级
     */
    @Override
    public CustomerLevelVo queryById(Long customerLevelId){
        return baseMapper.selectVoById(customerLevelId);
    }

    /**
     * 查询会员等级列表
     */
    @Override
    public TableDataInfo<CustomerLevelVo> queryPageList(CustomerLevelBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CustomerLevel> lqw = buildQueryWrapper(bo);
        Page<CustomerLevelVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询会员等级列表
     */
    @Override
    public List<CustomerLevelVo> queryList(CustomerLevelBo bo) {
        LambdaQueryWrapper<CustomerLevel> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CustomerLevel> buildQueryWrapper(CustomerLevelBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CustomerLevel> lqw = Wrappers.lambdaQuery();
        lqw.like(StringUtils.isNotBlank(bo.getLevelName()), CustomerLevel::getLevelName, bo.getLevelName());
        lqw.eq(StringUtils.isNotBlank(bo.getDescription()), CustomerLevel::getDescription, bo.getDescription());
        lqw.eq(StringUtils.isNotBlank(bo.getIcon()), CustomerLevel::getIcon, bo.getIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getLevelIcon()), CustomerLevel::getLevelIcon, bo.getLevelIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getSuccessIcon()), CustomerLevel::getSuccessIcon, bo.getSuccessIcon());
        lqw.eq(StringUtils.isNotBlank(bo.getBackground()), CustomerLevel::getBackground, bo.getBackground());
        lqw.eq(bo.getLevelValue() != null, CustomerLevel::getLevelValue, bo.getLevelValue());
        return lqw;
    }

    /**
     * 新增会员等级
     */
    @Override
    public Boolean insertByBo(CustomerLevelBo bo) {
        CustomerLevel add = BeanUtil.toBean(bo, CustomerLevel.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCustomerLevelId(add.getCustomerLevelId());
        }
        return flag;
    }

    /**
     * 修改会员等级
     */
    @Override
    public Boolean updateByBo(CustomerLevelBo bo) {
        CustomerLevel update = BeanUtil.toBean(bo, CustomerLevel.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CustomerLevel entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除会员等级
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
